package com.example.trader_svc.controller;

import com.example.trader_svc.model.PriceAdjustment;
import com.example.trader_svc.model.TraderLog;
import com.example.trader_svc.service.TraderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Tag(name = "交易操作", description = "债券价格调整与交易记录查询接口")
@RestController
@RequestMapping("/api/trade")
public class TraderController {

    @Autowired
    private TraderService traderService;

    @Operation(
            summary = "调整债券价格",
            description = "根据市场因素计算最终成交价格",
            responses = {
                    @ApiResponse(responseCode = "200", description = "价格计算成功"),
                    @ApiResponse(responseCode = "400", description = "无效的价格参数")
            }
    )
    @PostMapping("/price")
    public ResponseEntity<BigDecimal> adjustPrice(
            @Parameter(description = "价格调整参数", required = true)
            @RequestBody PriceAdjustment adj) {
        return ResponseEntity.ok(traderService.calculatePrice(adj));
    }

    @Operation(
            summary = "查询交易日志",
            description = "根据债券代码获取价格调整记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "404", description = "债券代码不存在")
            }
    )
    @GetMapping("/log")
    public ResponseEntity<List<TraderLog>> getLogs(
            @Parameter(description = "债券代码", example = "BOND2024")
            @RequestParam String bondCode) {
        return ResponseEntity.ok(traderService.getLogs(bondCode));
    }
}
